<template>
	<div class="app-container">
		<el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
			<el-form-item label="商户号" prop="merchantId">
				<el-input v-model="queryParams.merchantId" placeholder="请输入商户号" clearable size="small" @keyup.enter.native="handleQuery" />
			</el-form-item>
			<el-form-item label="商户名称" prop="merchantName">
				<el-input v-model="queryParams.merchantName" placeholder="请输入商户名称" clearable size="small" @keyup.enter.native="handleQuery" />
			</el-form-item>
			<el-form-item label="奖品名称" prop="prizeName">
				<el-input v-model="queryParams.prizeName" placeholder="请输入奖品名称" clearable size="small" @keyup.enter.native="handleQuery" />
			</el-form-item>
			<el-form-item label="奖品数量" prop="prizeAmount">
				<el-input v-model="queryParams.prizeAmount" placeholder="请输入奖品数量" clearable size="small" @keyup.enter.native="handleQuery" />
			</el-form-item>
			<el-form-item label="取奖地址" prop="prizeAddress">
				<el-input v-model="queryParams.prizeAddress" placeholder="请输入取奖地址" clearable size="small" @keyup.enter.native="handleQuery" />
			</el-form-item>
			<el-form-item>
				<el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
				<el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
			</el-form-item>
		</el-form>

		<el-row :gutter="10" class="mb8">
			<el-col :span="1.5">
				<el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd" v-hasPermi="['system:prizeinfo:add']">新增</el-button>
			</el-col>
			<el-col :span="1.5">
				<el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate" v-hasPermi="['system:prizeinfo:edit']">修改</el-button>
			</el-col>
			<el-col :span="1.5">
				<el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
				 v-hasPermi="['system:prizeinfo:remove']">删除</el-button>
			</el-col>
			<el-col :span="1.5">
				<el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport" v-hasPermi="['system:prizeinfo:export']">导出</el-button>
			</el-col>
			<right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
		</el-row>

		<el-table v-loading="loading" :data="prizeinfoList" @selection-change="handleSelectionChange">
			<el-table-column type="selection" width="55" align="center" />
			<el-table-column label="奖品号" align="center" prop="prizeId" />
			<el-table-column label="商户号" align="center" prop="merchantId" />
			<el-table-column label="商户名称" align="center" prop="merchantName" />
			<el-table-column label="奖品名称" align="center" prop="prizeName" />
			<el-table-column label="奖品数量" align="center" prop="prizeAmount" />
			<!-- <el-table-column label="奖品图片" align="center" prop="prizePic" /> -->
			<el-table-column label="取奖地址" align="center" prop="prizeAddress" />
			<el-table-column label="操作" align="center" class-name="small-padding fixed-width">
				<template slot-scope="scope">
					<el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)" v-hasPermi="['system:prizeinfo:edit']">修改</el-button>
					<el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)" v-hasPermi="['system:prizeinfo:remove']">删除</el-button>
				</template>
			</el-table-column>
		</el-table>

		<pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
		 @pagination="getList" />

		<!-- 添加或修改奖品对话框 -->
		<el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
			<el-form ref="form" :model="form" :rules="rules" label-width="80px">
				<el-form-item label="商户号" prop="merchantId">
					<el-input v-model="form.merchantId" placeholder="请输入商户号" disabled="disabled" />
				</el-form-item>
				<!-- <el-form-item label="商户名称" prop="merchantName">
					<el-input v-model="form.merchantName" placeholder="请输入商户名称" />
				</el-form-item> -->
				<el-form-item label="奖品名称" prop="prizeName">
					<el-input v-model="form.prizeName" placeholder="请输入奖品名称" />
				</el-form-item>
				<el-form-item label="奖品数量" prop="prizeAmount">
					<el-input v-model="form.prizeAmount" placeholder="请输入奖品数量" />
				</el-form-item>


				<el-form-item label="封面图片效果预览" prop="prizePic">
					<img :src="form.prizePic" alt="暂无图片" style="width: 220px;height: 150px;margin-top: 16px;margin-left: 30px;">
				</el-form-item>

				<el-form-item label="奖品图片" prop="prizePic">
					<el-upload class="upload-demo" :action="uploadFileUrl" :on-preview="handlePreview" :on-remove="handleRemove"
					 :file-list="fileList" list-type="picture" :on-success="handleUploadSuccess" :headers="headers" :limit="1">
						<el-button size="small" type="primary">点击上传</el-button>
						<div slot="tip" class="el-upload__tip">只能上传jpg/png文件，且不超过500kb</div>
					</el-upload>
				</el-form-item>

				<el-form-item label="取奖地址" prop="prizeAddress">
					<el-input v-model="form.prizeAddress" placeholder="请输入取奖地址" />
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button type="primary" @click="submitForm">确 定</el-button>
				<el-button @click="cancel">取 消</el-button>
			</div>
		</el-dialog>
	</div>
</template>

<script>
	import {
		listPrizeinfo,
		getPrizeinfo,
		delPrizeinfo,
		addPrizeinfo,
		updatePrizeinfo,
		exportPrizeinfo
	} from "@/api/system/prizeinfo";
	import {
		getToken
	} from "@/utils/auth";
	export default {
		name: "Prizeinfo",
		components: {},
		data() {
			return {
				//图片上传
				uploadFileUrl: process.env.VUE_APP_BASE_API + "/common/upload",
				headers: {
					Authorization: "Bearer " + getToken(),
				},
				fileList: [],
				// 遮罩层
				loading: true,
				// 选中数组
				ids: [],
				// 非单个禁用
				single: true,
				// 非多个禁用
				multiple: true,
				// 显示搜索条件
				showSearch: true,
				// 总条数
				total: 0,
				// 奖品表格数据
				prizeinfoList: [],
				// 弹出层标题
				title: "",
				// 是否显示弹出层
				open: false,
				// 查询参数
				queryParams: {
					pageNum: 1,
					pageSize: 10,
					merchantId: null,
					merchantName: null,
					prizeName: null,
					prizeAmount: null,
					prizePic: null,
					prizeAddress: null
				},
				// 表单参数
				form: {},
				// 表单校验
				rules: {
					merchantId: [{
						required: true,
						message: "商户号不能为空",
						trigger: "blur"
					}],
					merchantName: [{
						required: true,
						message: "商户名称不能为空",
						trigger: "blur"
					}],
					prizeName: [{
						required: true,
						message: "奖品名称不能为空",
						trigger: "blur"
					}],
					prizeAmount: [{
						required: true,
						message: "奖品数量不能为空",
						trigger: "blur"
					}],
					prizeAddress: [{
						required: true,
						message: "取奖地址不能为空",
						trigger: "blur"
					}]
				}
			};
		},
		created() {
			this.getList();
		},
		methods: {
			/* 上传 */
			handleRemove(file, fileList) {
				console.log(file, fileList);
			},
			handlePreview(file) {
				console.log(file);
			},
			// 上传成功回调
			handleUploadSuccess(res, file) {
				this.$message.success("上传成功");
				this.$emit("input", res.url);
				this.form.prizePic = res.url;
				console.log(this.form.prizePic)
			},

			/** 查询奖品列表 */
			getList() {
				this.queryParams.merchantId = this.$route.query.merchantId;
				this.loading = true;
				listPrizeinfo(this.queryParams).then(response => {
					this.prizeinfoList = response.rows;
					this.total = response.total;
					this.loading = false;
				});
			},
			// 取消按钮
			cancel() {
				this.open = false;
				this.reset();
			},
			// 表单重置
			reset() {
				this.form = {
					prizeId: null,
					merchantId: null,
					merchantName: null,
					prizeName: null,
					prizeAmount: null,
					prizePic: null,
					prizeAddress: null
				};
				this.resetForm("form");
			},
			/** 搜索按钮操作 */
			handleQuery() {
				this.queryParams.pageNum = 1;
				this.getList();
			},
			/** 重置按钮操作 */
			resetQuery() {
				this.resetForm("queryForm");
				this.handleQuery();
			},
			// 多选框选中数据
			handleSelectionChange(selection) {
				this.ids = selection.map(item => item.prizeId)
				this.single = selection.length !== 1
				this.multiple = !selection.length
			},
			/** 新增按钮操作 */
			handleAdd() {
				this.reset();
				this.form.merchantId = this.queryParams.merchantId;
				this.open = true;
				this.title = "添加奖品";
			},
			/** 修改按钮操作 */
			handleUpdate(row) {
				debugger
				this.reset();
				const prizeId = row.prizeId || this.ids
				console.log(prizeId)
				
				getPrizeinfo(prizeId).then(response => {
					this.form = response.data;
					this.open = true;
					this.title = "修改奖品";
				});
			},
			/** 提交按钮 */
			submitForm() {
				this.$refs["form"].validate(valid => {
					if (valid) {
						if (this.form.prizeId != null) {
							updatePrizeinfo(this.form).then(response => {
								this.msgSuccess("修改成功");
								this.open = false;
								this.getList();
								this.fileList= [];
							});
						} else {
							addPrizeinfo(this.form).then(response => {
								this.msgSuccess("新增成功");
								this.open = false;
								this.getList();
								this.fileList= [];
							});
						}
					}
				});
			},
			/** 删除按钮操作 */
			handleDelete(row) {
				const prizeIds = row.prizeId || this.ids;
				this.$confirm('是否确认删除奖品编号为"' + prizeIds + '"的数据项?', "警告", {
					confirmButtonText: "确定",
					cancelButtonText: "取消",
					type: "warning"
				}).then(function() {
					return delPrizeinfo(prizeIds);
				}).then(() => {
					this.getList();
					this.msgSuccess("删除成功");
				})
			},
			/** 导出按钮操作 */
			handleExport() {
				const queryParams = this.queryParams;
				this.$confirm('是否确认导出所有奖品数据项?', "警告", {
					confirmButtonText: "确定",
					cancelButtonText: "取消",
					type: "warning"
				}).then(function() {
					return exportPrizeinfo(queryParams);
				}).then(response => {
					this.download(response.msg);
				})
			}
		}
	};
</script>
